﻿using System;
using System.Timers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FancyBattles.GameEntities
{
    class Archer : BouncingBitch
    {
        #region Atributes

        private static Texture2D idle;
        private static Texture2D selected;
        private static Texture2D confirmed;
        private static Texture2D action;
        private static Texture2D hit;
        private static Texture2D victory;
        private static Texture2D death;
        private static Texture2D stun;
        private static Texture2D heal;
        private static Texture2D blueGlow;
        private static Texture2D redGlow;
        private Texture2D currentGlow;

        private Timer animationTimer;

        #endregion

        #region Properties

        public static Texture2D Idle
        {
            internal get { return idle; }
            set { idle = value; }
        }

        public static Texture2D Selected
        {
            internal get { return selected; }
            set { selected = value; }
        }

        public static Texture2D Confirmed
        {
            internal get { return confirmed; }
            set { confirmed = value; }
        }

        public static Texture2D Action
        {
            internal get { return action; }
            set { action = value; }
        }

        public static Texture2D Hit
        {
            internal get { return hit; }
            set { hit = value; }
        }

        public static Texture2D Victory
        {
            internal get { return victory; }
            set { victory = value; }
        }

        public static Texture2D Death
        {
            internal get { return death; }
            set { death = value; }
        }

        public static Texture2D Stun
        {
            internal get { return stun; }
            set { stun = value; }
        }

        public static Texture2D Heal
        {
            internal get { return heal; }
            set { heal = value; }
        }
        
        public static Texture2D BlueGlow
        {
            internal get { return blueGlow; }
            set { blueGlow = value; }
        }

        public static Texture2D RedGlow
        {
            internal get { return redGlow; }
            set { redGlow = value; }
        }

        #endregion

        #region Creators

        public Archer(Vector2 position, PlayerIndex player)
            : base(position, player)
        {
            animationTimer = new System.Timers.Timer(2000);
            animationTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            Texture = Idle;
        }

        #endregion

        #region Methods

        public override void ChangeTexture(animationState a)
        {
            if (!IsStunned)
            {
                switch (a)
                {
                    case animationState.Idle:
                        if (Texture != Confirmed)
                            Texture = Idle;
                        break;
                    case animationState.Confirmed:
                        Texture = Confirmed;
                        break;
                    case animationState.Selected:
                        if (Texture != Confirmed)
                            Texture = Selected;
                        break;
                    case animationState.Action:
                        Texture = Action;
                        break;
                    case animationState.Hit:
                        Texture = Hit;
                        animationTimer.Start();
                        break;
                    case animationState.Victory:
                        Texture = Victory;
                        break;
                    case animationState.Death:
                        Texture = Death;
                        break;
                    case animationState.Stun:
                        Texture = Stun;
                        break;
                    case animationState.Heal:
                        Texture = Heal;
                        break;
                    default:
                        Texture = Idle;
                        break;
                }
            }
        }

        public override void Draw()
        {
            base.Draw();

            //sets the glow
            if (Player.Equals(PlayerIndex.One))
            {
                currentGlow = blueGlow;
            }
            else
            {
                currentGlow = redGlow;
            }

            SpriteBatch.Draw(currentGlow, new Rectangle((int)Position.X - (int)BITCH_RADIUS, (int)Position.Y - (int)BITCH_RADIUS, 90, 90), Color.White);

            if (Texture == null)
            {
                Texture = Idle;
            }

            Color selectedColor = Color.White;
            
            if(Texture != null)
                SpriteBatch.Draw(Texture, new Vector2(Position.X - BITCH_RADIUS, Position.Y - BITCH_RADIUS), selectedColor);
        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            animationTimer.Stop();
            ChangeTexture(animationState.Action);
        }

        #endregion
    }
}
